# 装饰器
import os

import jmespath
from common.request_util import send_request
from common.yaml_util import write_glo, clear_glo, clear_env
import pytest


# -----------------------------------------------工程前置动作---------------------------------------------------------
@pytest.fixture(scope='session', autouse=True)
def project_pro():
    # 清除全局变量
    clear_glo()

    # 清除日志数据
    root_dir = os.path.dirname(__file__)
    log_path = rf'{root_dir}\log\apiLog.log'
    with open(log_path, 'w') as f:
        f.truncate(0)

    # 获取前后台token
    method = 'POST'
    url = 'http://8.140.193.160:8899/api/login'
    headers = {"content-type": "application/json"}
    body = {"account": "18141200880", "password": "Aa123456!"}
    r = send_request(name='前台登录', method=method, url=url, headers=headers, json=body)
    # print(r.json()['data']['token'])
    token = jmespath.search('data.token', r.json())
    # 写在全局变量中
    write_glo({"pc_token": token})

    # 发送后台登录请求，获取后台token
    method = 'POST'
    url = 'http://8.140.193.160:8899/adminapi/login'
    headers = {"content-type": "application/json"}
    body = {"account": "admin", "pwd": "920509", "imgcode": "0"}
    r = send_request(name='后台登录', method=method, url=url, headers=headers, json=body)
    token = jmespath.search('data.token', r.json())
    # 写在全局变量中
    write_glo({"manage_token": token})


# -----------------------------------------------所有工程中每条用例的前置动作---------------------------------------------------------


@pytest.fixture(scope='function', autouse=True)
def clear_env_var():
    clear_env()


# if __name__ == '__main__':
#     project_pro(